package com.hui.admin.controller;

import com.hui.admin.annotation.Log;
import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.dto.request.CategoryPageRequestDTO;
import com.hui.admin.dto.request.CategorySaveRequestDTO;
import com.hui.admin.dto.response.CategoryPageResponseDTO;
import com.hui.admin.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 文章分类控制层
 */
@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;


    /**
     * 新增分类
     */
    @Log("新增分类")
    @PostMapping
    public Result<Void> createCategory(@RequestBody CategorySaveRequestDTO requestDTO) {
        categoryService.createCategory(requestDTO);
        return Results.success();
    }

    /**
     * 获取分类列表
     */
    @GetMapping
    public Result<CategoryPageResponseDTO> getCategoryList(CategoryPageRequestDTO requestDTO) {
        return Results.success(categoryService.getCategoryList(requestDTO));
    }

    /**
     * 删除分类
     */
    @Log("删除分类")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Results.success();
    }

    /**
     * 批量删除文章
     */
    @Log("批量删除分类")
    @PostMapping("/deleteBatch")
    public Result<Void> deleteArticles(@RequestBody Map<String, Long[]> payload) {
        Long[] ids = payload.get("ids");
        categoryService.deleteArticles(ids);
        return Results.success();
    }

    /**
     * 修改文章分类
     */
    @Log("修改分类")
    @PutMapping
    public Result<Void> updateCategory(@RequestBody CategorySaveRequestDTO requestDTO) {
        categoryService.updateCategory(requestDTO);
        return Results.success();
    }
}
